#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/wait.h>

typedef struct STUDENT {
    int sid;
    char name[100];
}Stu;

int main(int argc, char* argv[])
{
    if(argc != 2) {
        printf("filename number error\n");
        exit(1);
    }

    int fd = open(argv[1], O_RDWR);
    if(-1 == fd) {
        perror("open error");
        exit(1);
    }
    
    int len = sizeof(Stu);
    
    Stu* s = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(s == MAP_FAILED) {
        perror("mmap error");
        exit(1);
    }

    // 读数据
    while(1) {
        printf("sid = %d, name = %s\n", s->sid, s->name);
        sleep(1);
    }
    
    munmap(s, len);
    close(fd);

    return 0;
}

